import {
  getToken,
  feedback
} from '../../../api.js'
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pci_img: '',
    showImages: [], //显示图片,
    addEdit: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  //添加图片
  addImg() {
    var postData = {};
    var _this = this;
    getToken().then(res => {
      if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
      postData.key = res.data.data.key;
      postData.token = res.data.data.token
      console.log('postData', postData)
      wx.chooseImage({
        count: 1, //最多可以选择的图片张数
        sizeType: ['original', 'compressed'], //所选的图片的尺寸
        sourceType: ['album', 'camera'], //选择图片的来源
        success: function (res) {
          wx.showLoading({
            title: '正在上传',
          })
          var tempFilePaths = res.tempFilePaths[0];
          var type = tempFilePaths.substring(tempFilePaths.lastIndexOf("."), tempFilePaths.length);
          postData.key = postData.key + type;
          wx.uploadFile({
            url: 'https://upload.qiniup.com', //服务器地址
            filePath: tempFilePaths, //要上传文件资源的路径
            name: 'file', //文件的key 
            header: {
              "Content-Type": "multipart/form-data"
            },
            formData: postData, //传递参数
            success: function (res) {
              wx.hideLoading()
              console.log('七牛返回值', res)
              var data = JSON.parse(res.data)
              var showImages = _this.data.showImages;
              showImages.push(tempFilePaths)
              var image = _this.data.addEdit;

              var singleImg = {};
              singleImg.pic_url = data.key
              if (_this.data.isEdit) {
                singleImg.id = '';
              }
              console.log('singleImg', singleImg)
              image.push(singleImg)
              _this.setData({
                showImages,
                'addEdit': image
              })
            },
          })
        },
      })
    })
  },

  //替换图片
  changeImg(e) {
    var postData = {};
    var _this = this;
    getToken().then(res => {
      if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
      postData.key = res.data.data.key;
      postData.token = res.data.data.token
      console.log('postData', postData)
      wx.chooseImage({
        count: 1, //最多可以选择的图片张数
        success: function (res) {
          var tempFilePaths = res.tempFilePaths[0];
          var type = tempFilePaths.substring(tempFilePaths.lastIndexOf("."), tempFilePaths.length);
          postData.key = postData.key + type;
          wx.uploadFile({
            url: 'https://upload.qiniup.com', //服务器地址
            filePath: tempFilePaths, //要上传文件资源的路径
            name: 'file', //文件的key 
            header: {
              "Content-Type": "multipart/form-data"
            },
            formData: postData, //传递参数
            success: function (res) {
              console.log('七牛返回值', res)
              var data = JSON.parse(res.data)
              var showImages = _this.data.showImages;
              showImages[e.currentTarget.dataset.key] = tempFilePaths
              var image = _this.data.addEdit;
              image[e.currentTarget.dataset.key].pic_url = (data.key)
              if (_this.data.isEdit) {
                image[e.currentTarget.dataset.key].id = '';
              }
              _this.setData({
                showImages,
                'addEdit': image
              })
            },
          })
        },
      })
    })
  },
  formSubmit: function (e) {
    var that = this;
    var images = that.data.addEdit
    feedback({
      token: app.globalData.userInfo.token,
      content: e.detail.value.textarea,
      contact: e.detail.value.contact,
      image: images
    }).then(res => {
      if (res.data.code == 666) {
        wx.showToast({
          title: res.data.msg,
        })
        setTimeout(function () {
          wx.switchTab({
            url: '/pages/tabBar/my/my',
          })
        }, 1500)
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },
})